package com.yuexin.project.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.project.system.notice.service.INoticeService;
import com.yuexin.project.system.noticeUser.domain.NoticeUser;
import com.yuexin.project.system.noticeUser.service.INoticeUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Ahanu
 */
@Api("手机端公告管理")
@RestController
@RequestMapping("/meals/mobile")
public class NoticeForMobileAPIController extends BaseController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private INoticeUserService noticeUserService;

    @GetMapping("/getNoticeById")
    @ApiOperation("根据ID获取公告")
    public Result selectNoticeById(String json) {
        Long id = Long.parseLong(parseJson(json, "noticeId").toString());
        return new Result(ResultCode.SUCCESS, noticeService.selectNoticeById(id));
    }

    @GetMapping("/getNotices")
    @ApiOperation("获取公告列表")
    public Result selectNoticeByUserName(String json) {
        JSONObject jobj = JSON.parseObject(json);
        JSONObject data = (JSONObject) jobj.get("data");
        JSONObject token = data.getJSONObject("token");
        String companyId = token.getString("companyId");
        String userName = token.getString("userName");
        PageHelper.startPage(data.getInteger("pageNum"), data.getInteger("pageSize"));
        startPage();
        // 注：status（0:未阅，1：已阅）
        return new Result(ResultCode.SUCCESS, getDataTable(noticeService.selectNoticeByUserName(userName, companyId)));
    }

    @PostMapping("/readNotice")
    @ApiOperation("阅读公告")
    public Result selectReadNotice(String json) {
        JSONObject token = (JSONObject) parseJson(json, "token");
        NoticeUser noticeUser = new NoticeUser();
        noticeUser.setNoticeId(Integer.parseInt(parseJson(json, "noticeId").toString()));
        noticeUser.setCompanyId(token.getString("companyId"));
        noticeUser.setUserName(token.getString("userName"));
        noticeUser.setStatus("0");
        List<NoticeUser> noticeUserList = noticeUserService.selectNoticeUserList(noticeUser);
        if(noticeUserList == null || noticeUserList.isEmpty()){
            noticeUserService.insertNoticeUser(noticeUser);
        }
        return new Result(ResultCode.SUCCESS);
    }

    private Object parseJson(String json, String param){
        JSONObject jobj = JSON.parseObject(json);
        JSONObject data = (JSONObject) jobj.get("data");
        return data.get(param);
    }
}
